<?php

namespace JingdongApi\Models;

/**
 * 商户信息模型
 * 
 * @package JingdongApi\Models
 */
class MerchantInfo
{
    /**
     * 商户名称
     * @var string
     */
    public $merchantName;
    
    /**
     * 商户电话
     * @var string
     */
    public $merchantPhone;
    
    /**
     * 商户地址
     * @var string
     */
    public $merchantAddress;
    
    /**
     * 营业执照
     * @var string
     */
    public $businessLicense;
    
    /**
     * 联系人
     * @var string
     */
    public $contactPerson;
    
    /**
     * 联系电话
     * @var string
     */
    public $contactPhone;
    
    /**
     * 商户类型
     * @var string
     */
    public $merchantType;
    
    /**
     * 构造函数
     * 
     * @param array $data 商户数据
     */
    public function __construct($data = [])
    {
        $this->merchantName = $data['merchant_name'] ?? '';
        $this->merchantPhone = $data['merchant_phone'] ?? '';
        $this->merchantAddress = $data['merchant_address'] ?? '';
        $this->businessLicense = $data['business_license'] ?? '';
        $this->contactPerson = $data['contact_person'] ?? '';
        $this->contactPhone = $data['contact_phone'] ?? '';
        $this->merchantType = $data['merchant_type'] ?? '1';
    }
    
    /**
     * 转换为数组
     * 
     * @return array
     */
    public function toArray()
    {
        return [
            'merchant_name' => $this->merchantName,
            'merchant_phone' => $this->merchantPhone,
            'merchant_address' => $this->merchantAddress,
            'business_license' => $this->businessLicense,
            'contact_person' => $this->contactPerson,
            'contact_phone' => $this->contactPhone,
            'merchant_type' => $this->merchantType
        ];
    }
    
    /**
     * 验证必填字段
     * 
     * @throws \InvalidArgumentException
     */
    public function validate()
    {
        $required = [
            'merchantName' => '商户名称',
            'merchantPhone' => '商户电话',
            'merchantAddress' => '商户地址',
            'businessLicense' => '营业执照',
            'contactPerson' => '联系人',
            'contactPhone' => '联系电话'
        ];
        
        foreach ($required as $field => $desc) {
            if (empty($this->$field)) {
                throw new \InvalidArgumentException($desc . ' 不能为空');
            }
        }
    }
}
